use wasm_bindgen::{JsCast, UnwrapThrowExt};
use web_sys::{window, Event, HtmlInputElement, HtmlTextAreaElement, InputEvent};

pub fn get_value_from_input_event(e: InputEvent) -> String {
    let event: Event = e.dyn_into().unwrap_throw();
    let event_target = event.target().unwrap_throw();
    let target: HtmlInputElement = event_target.dyn_into().unwrap_throw();
    web_sys::console::log_1(&target.value().into());
    target.value()
}

pub fn get_input_value(id: &'static str) -> Option<String> {
    if let Some(window) = window() {
        // 获取 document 对象
        if let Some(document) = window.document() {
            // 通过 ID 获取元素
            if let Some(element) = document.get_element_by_id(id) {
                // 转换为 HtmlInputElement
                if let Ok(input) = element.dyn_into::<HtmlInputElement>() {
                    let value = input.value();
                    return Some(String::from(value));
                }
            }
        }
    }
    None
}

pub fn set_input_value(id: &'static str, value: &str) {
    if let Some(window) = window() {
        // 获取 document 对象
        if let Some(document) = window.document() {
            // 通过 ID 获取元素
            if let Some(element) = document.get_element_by_id(id) {
                // 转换为 HtmlInputElement
                if let Ok(input) = element.dyn_into::<HtmlInputElement>() {
                    input.set_value(value);
                    log::info!("set input value: {}", value);
                }
            }
        }
    }
}


pub fn get_textarea_value(id: &'static str) -> Option<String> {
    if let Some(window) = window() {
        // 获取 document 对象
        if let Some(document) = window.document() {
            // 通过 ID 获取元素
            if let Some(element) = document.get_element_by_id(id) {
                // 转换为 HtmlInputElement
                if let Ok(input) = element.dyn_into::<HtmlTextAreaElement>() {
                    let value = input.value();
                    return Some(String::from(value));
                }
            }
        }
    }
    None
}

pub fn set_textarea_value(id: &'static str, value: &str) {
    if let Some(window) = window() {
        // 获取 document 对象
        if let Some(document) = window.document() {
            // 通过 ID 获取元素
            if let Some(element) = document.get_element_by_id(id) {
                // 转换为 HtmlInputElement
                if let Ok(input) = element.dyn_into::<HtmlTextAreaElement>() {
                    input.set_value(value);
                }
            }
        }
    }
}

pub fn click_node(id: &'static str) {
    if let Some(window) = window() {
        // 获取 document 对象
        if let Some(document) = window.document() {
            // 通过 ID 获取元素
            if let Some(element) = document.get_element_by_id(id) {
                // 转换为 HtmlInputElement
                if let Ok(input) = element.dyn_into::<HtmlInputElement>() {
                    input.click();
                }
            }
        }
    }
}